﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace OneBarCode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtPath.Text) || !Directory.Exists(txtPath.Text))
            {
                MessageBox.Show("请选择保存路径", "提示",MessageBoxButtons.OK);
                return;
            }
            long start = 0;
            long end = 0;
            if (string.IsNullOrEmpty(txtStart.Text) || string.IsNullOrEmpty(txtEnd.Text) || !long.TryParse(txtEnd.Text, out end) || !long.TryParse(txtStart.Text, out start)) 
            {
                MessageBox.Show("请输入数据范围", "提示", MessageBoxButtons.OK);
                return;
            }
            if (start > end)
            {
                MessageBox.Show("请输入有效的数据范围", "提示", MessageBoxButtons.OK);
                return;
            }
            //

            DrawingBarCode drawing = new DrawingBarCode();
            drawing.Height = 90;
            drawing.Magnify = 1;
            drawing.DrawingBarCodePre(true);
            Image img = null;

            for (long i = start; i <= end; i++)
            {
                img = drawing.DrawingBarCode39(i.ToString(), 12, true);
                //FileStream filestream = new FileStream(txtPath.Text+"/"+i+".jpg", FileMode.CreateNew);
                img.Save(txtPath.Text + "/" + i + ".jpg");
            }
            MessageBox.Show("生成成功！", "提示", MessageBoxButtons.OK);
                
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            String strText=txtStart.Text;
            BarCodeProvider provider=BarCodeProvider.Intance;
            Bitmap barcode=provider.CreateBarCodeImage(strText);
            DrawingBarCode drawing = new DrawingBarCode();
            drawing.Height = 90;
            drawing.Magnify = 1;
            drawing.DrawingBarCodePre(true);
            Image b = drawing.DrawingBarCode39(strText, 12, false);
            //pictureBox1.Image = Image.FromHbitmap(barcode.GetHbitmap());
            pictureBox1.Image = b;
            if (!string.IsNullOrEmpty(txtPath.Text) &&Directory.Exists(txtPath.Text))
            {
                b.Save(txtPath.Text + "/" + strText + ".jpg");
                MessageBox.Show("生成成功！", "提示", MessageBoxButtons.OK);
            }
        }

        private void btnSelPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
            if(folderDialog.ShowDialog()==DialogResult.OK)
            {
                txtPath.Text = folderDialog.SelectedPath;
            }
            
        }
    }
}
